// pages/staff_mine/edit_address/apply.js

let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        region_1_list: new Array(),
        region_1_name: '',
        region_2_list: new Array(),
        region_2_name: '',
        region_3_list: new Array(),
        region_3_name: '',
        region_select: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 获取地区
        app.execute(function() {
            let page = app.getPage();
            page.getRegion(page, 1, 0);
        });
    },

    /**
     * 自定义函数--改变选择器
    */
    changePicker: function(e) {
        let grade = parseInt(e.currentTarget.dataset.grade);
        let value = this.data['region_' + e.currentTarget.dataset.grade + '_list'][e.detail.value];
        let change = new Object();
        switch(grade){
            case 1:
                change['region_1_name'] = value.name;
                change['region_2_list'] = new Array();
                change['region_2_name'] = '';
                change['region_3_list'] = new Array();
                change['region_3_name'] = '';
                change['region_select'] = 0;
                this.getRegion(this, 2, value.id);
                break;
            case 2:
                change['region_2_name'] = value.name;
                change['region_3_list'] = new Array();
                change['region_3_name'] = '';
                change['region_select'] = 0;
                this.getRegion(this, 3, value.id);
                break;
            case 3:
                change['region_3_name'] = value.name;
                change['region_select'] = value.id;
                break;
        };
        this.setData(change);

    },

    /**
     * 自定义函数--获取地区
    */
    getRegion: function(page, grade, parent_id) {
        let data = {
            grade: grade,
            parent_id: parent_id
        };
        wx.showLoading({
            title: '获取信息...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_region/get_grade',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: data,
            success: function (response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        let change = new Object();
                        change['region_' + grade + '_list'] = response.data.data.region_list;
                        page.setData(change);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--提交保存
    */
    submit: function() {
        
        let data = {
            region_id: this.data.region_select,
        };

        if(0 === data.region_id){
            wx.showToast({
                title: '请选中区县',
                icon: 'none'
            });
        };

        wx.showLoading({
            title: '提交申请...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_region/apply',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: data,
            success: function (response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        wx.showToast({
                            title: '提交成功',
                        });
                        setTimeout(function(){
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });


    }

});